package com.xiaoxu.logProject.utils

import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{LongType, StringType, StructField, StructType}

/**
  * 输入：访问时间、访问URL、耗费的流量、访问IP地址信息
  * 输出：URL、cmsType(video/article)、cmsId(编号)、流量、ip、城市信息、访问时间、天
  * 访问日志转换工具类 输入==>输出
  *
  * createTime: 2018-12-08 16:02
  **/
object LogConvertUtil {
  //定义输出的字段结构
  val structFieldArray = Array(
    StructField("url", StringType),
    StructField("cmsType", StringType),
    StructField("cmsId", LongType),
    StructField("traffic", LongType),
    StructField("ip", StringType),
    StructField("city", StringType),
    StructField("time", StringType),
    StructField("day", StringType)
  )

  val struct = StructType(structFieldArray)


  /**
    * 根据每一行输入的信息转换为输出的的样式
    *
    * @param log
    */
  def parseLog(log: String):Row = {
    try{
      val splits = log.split("\t")

      val url = splits(1)
      val traffic = splits(2).toLong
      val ip = splits(3)

      val domain = "http://www.imooc.com/"

      //http://www.imooc.com/article/17891
      val cms = url.substring(url.indexOf(domain) + domain.length)
      val cmsTypeAndIds = cms.split("/")

      var cmsType = ""
      var cmsId = 0l
      if(cmsTypeAndIds.length > 1) {
        cmsType = cmsTypeAndIds(0)
        cmsId = cmsTypeAndIds(1).toLong
      }

      val city = "项城市"
      val time = splits(0)
      //2018-05-11 20:56:29
      val day = time.substring(0,10).replaceAll("-","")

      //这个row里面的字段要和struct中的字段对应上
      Row(url, cmsType, cmsId, traffic, ip, city, time, day)
    } catch {
      case e:Exception => Row("","",0L,0L,"","","","")
    }
  }

}
